package concurrent;

import junit.framework.TestCase;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangxin
 * @date 2020/9/29
 */
public class VolatileTest extends TestCase {

    //private volatile int i = 0;
    private volatile AtomicInteger i = new AtomicInteger(0);

    public void test_a() {
        Thread thread = new Thread(() -> {
            int t = 0;
            while (i.getAndIncrement() < 10000) {
                t++;
            }
            System.out.println(t);
        });

        Thread thread1 = new Thread(() -> {
            int t = 0;
            while (i.getAndIncrement() < 10000) {
                t++;
            }
            System.out.println(t);
        });

        thread.start();
        thread1.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
